# -*-coding: utf-8 -*-
"""
    @Theme   : 文档字段验证
    @Time    : 2024/6/9 10:41
    @Author  : YamFish
    @Url     : https://www.zhihu.com/people/yamfish
"""
import re

FIELD_LIST = {'name', 'age', 'birthday', 'current_home', 'origin_home'}


def check_integrity(insert):
    if not insert or FIELD_LIST != set(insert.keys()):
        return '字段不完整'


def check_update(update):
    if 'people_id' not in update:
        return '字段不完整'
    return check_integrity(update.get('updated_info', {}))


def check_fields(insert):
    if not insert['name']:
        return '姓名不能为空'

    age = insert['age']
    if not isinstance(age, int) or age < 0 or age > 120:
        return '年龄必需是范围在0到120之间的整数'

    if not re.match(r'\d{4}-\d{2}-\d{2}', insert['birthday']):
        return '生日格式必需为：yyyy-mm-dd'


def transfer_id(people_id):
    if isinstance(people_id, int):
        return people_id
    try:
        people_id = int(people_id)
        return people_id
    except ValueError:
        return -1
